package com.example.gybicycle.service.wx;

import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.enums.NumType;
import com.example.gybicycle.common.util.LoginUtil;
import com.example.gybicycle.common.util.NumberUtil;
import com.example.gybicycle.dao.MemberDao;
import com.example.gybicycle.dao.SysUserDao;
import com.example.gybicycle.entity.Member;
import com.example.gybicycle.entity.SysUser;
import com.example.gybicycle.entity.wx.WXLoginInfo;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class WLoginService {
    @Autowired
    private SysUserDao userDao;
    @Autowired
    private MemberDao memberDao;
    public WXLoginInfo login(WXLoginInfo info){
        WXLoginInfo info1=LoginUtil.getWxLoginInfo("eqweqwej"+info.getWxOpenid());
        if(info1!=null)
            return info1;
        if(info.getPassword()!=null&&info.getPassword().length()>0){
            SysUser user=userDao.login(info.getName(),info.getPassword());
            if(user!=null&&user.getFrozen().equals(true)){
                info.setName(user.getNickName());
                info.setBirthday(user.getBirthday());
                info.setType(1);
                info.setId(user.getId());
            }else
                return null;
        }else {
            Member member=memberDao.getByOpenId(info.getWxOpenid());
            if(member==null){
                BeanUtils.copyProperties(info,member);
                member.setId(NumberUtil.getNum(NumType.MEMBER));
                member.setGrade(0);
                memberDao.insert(member);
                info.setId(member.getId());
            }
        }
        info.setAuthorization("eqweqwej"+info.getWxOpenid());
        LoginUtil.setWxLoginInfo(info);
        return info;
    }

}
